package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemGuideTicketBinding;
import com.jwdf.uda.databinding.ItemSystemTicketBinding;
import com.jwdf.uda.databinding.ItemTicketNewBinding;
import com.jwdf.uda.utils.DateUtils;

/**
 * Created by Administrator on 2016/7/12.
 */
public class TicketsAdapter extends AbsAdapter<PbBean.PbGuideTicket> {
    private static final int ITEM_NEW_USER = 0;
    private static final int ITEM_SYSTEM = 1;
    private static final int ITEM_GUIDE = 2;

    public TicketsAdapter(Context context) {
        super(context);
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case ITEM_GUIDE:
                return new GuideTicket(mInflater.inflate(R.layout.item_guide_ticket, parent, false));
            case ITEM_SYSTEM:
                return new SystemTicket(mInflater.inflate(R.layout.item_system_ticket, parent, false));
            default:
                return new NewTicket(mInflater.inflate(R.layout.item_ticket_new, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        PbBean.PbGuideTicket bean = getItem(position);
        if (bean.getDesc().contains("搭配")) {
            return ITEM_GUIDE;
        } else if (bean.getDesc().contains("系统")) {
            return ITEM_SYSTEM;
        } else {
            return ITEM_NEW_USER;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    private class NewTicket extends AbsViewHolder<PbBean.PbGuideTicket> {

        private ItemTicketNewBinding mBinding;

        public NewTicket(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(PbBean.PbGuideTicket item) {
            mBinding.tvCreattime.setText(DateUtils.getDate(1000l*item.getCreateTime()));
            mBinding.tvOutdate.setText(convertView.getContext().getString(R.string.text_out_date, "2016-10-01"));
            mBinding.tvTicketCout.setText("" + item.getTicketNum());
            mBinding.tvDesc.setText(item.getDesc());
        }
    }

    private class SystemTicket extends AbsViewHolder<PbBean.PbGuideTicket> {

        private ItemSystemTicketBinding mBinding;

        public SystemTicket(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(PbBean.PbGuideTicket item) {
            mBinding.tvCreattime.setText(DateUtils.getDate(1000l*item.getCreateTime()));
            mBinding.tvTicketCout.setText("" + item.getTicketNum());
            mBinding.tvDesc.setText(item.getDesc());
        }
    }

    private class GuideTicket extends AbsViewHolder<PbBean.PbGuideTicket> {

        private ItemGuideTicketBinding mBinding;

        public GuideTicket(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(PbBean.PbGuideTicket item) {
            mBinding.tvCreattime.setText(DateUtils.getDate(1000l*item.getCreateTime()));
            mBinding.tvOutdate.setText(convertView.getContext().getString(R.string.text_out_date, "2016-10-01"));
            if(item.getTicketNum()>0){
                mBinding.ivAdd.setImageResource(R.mipmap.ic_ticket_add);
            }else{
                mBinding.ivAdd.setImageResource(R.mipmap.ic_ticket_cost);
            }
            mBinding.tvTicketCout.setText("" + Math.abs(item.getTicketNum()));
            mBinding.tvDesc.setText(item.getDesc());
        }
    }
}
